import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { Article } from './Article';
import { Collection } from './Collection';
import { UserRole } from './UserRole';

@Entity('user', { schema: 'blog_back' })
export class User {
  @PrimaryGeneratedColumn({
    type: 'int',
    name: 'id',
    comment: '主键id',
    unsigned: true,
  })
  id: number;

  @Column('varchar', { name: 'email', length: 255 })
  email: string;

  @Column('varchar', { name: 'username', comment: '用户名', length: 31 })
  username: string;

  @Column('varchar', {
    name: 'password',
    comment: '密码',
    length: 63,
    select: false,
  })
  password: string;

  @Column('varchar', {
    name: 'nickname',
    nullable: true,
    comment: '用户昵称',
    length: 255,
  })
  nickname: string | null;

  @Column('varchar', {
    name: 'avatar_image',
    nullable: true,
    comment: '头像',
    length: 255,
  })
  avatarImage: string | null;

  @Column('timestamp', {
    name: 'create_time',
    comment: '创建时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createTime: Date;

  @Column('timestamp', {
    name: 'update_time',
    comment: '更新时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updateTime: Date;

  @OneToMany(() => Article, (article) => article.user)
  articles: Article[];

  @OneToMany(() => Collection, (collection) => collection.user)
  collections: Collection[];

  @OneToMany(() => UserRole, (userRole) => userRole.user)
  userRoles: UserRole[];
}
